package com.example.myapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;


import com.bumptech.glide.Glide;

import java.util.ArrayList;




/**
 * Created by Administrator on 2017/6/3.
 */

public class GridAdapter extends BaseAdapter {


    private ArrayList<PhotoInfo> list;
    private LayoutInflater inflater;
    private Context mContext;
    public GridAdapter(ArrayList<PhotoInfo> list, Context mContext){
        this.mContext=mContext;
        this.list=list;
        this.inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    public ArrayList<PhotoInfo> getList() {
        return list;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        convertView = inflater.inflate(R.layout.item_image, parent, false);
        AbsListView.LayoutParams params = (AbsListView.LayoutParams) convertView.getLayoutParams();
        if (params == null) {
            Log.d("gggggg","Ggggggg");
            params = new AbsListView.LayoutParams((int)parent.getWidth() / 3, (int)parent.getWidth() / 3);
            convertView.setLayoutParams(params);
        } else {
            params.height = (int)parent.getWidth() / 3;
            params.width = (int)parent.getWidth() / 3;
        }
        final CheckBox checkBox=(CheckBox)convertView.findViewById(R.id.cb_image_selected);
        checkBox.setClickable(false);
        ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkBox.setChecked(!checkBox.isChecked());
                list.get(position).setChecked(checkBox.isChecked());
                Log.d("aaa",String.valueOf(list.get(position).isChecked()));
            }
        });
        Glide.with(mContext)
                .load(list.get(position).getUrl())
                .placeholder(R.mipmap.ic_gf_default_photo)
                .centerCrop()
                .crossFade()
                .error(R.mipmap.fail)
                .into(imageView);
       /* ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_image, parent, false);
            AbsListView.LayoutParams params = (AbsListView.LayoutParams) convertView.getLayoutParams();
            if (params == null) {
                Log.d("gggggg","Ggggggg");
                params = new AbsListView.LayoutParams((int)parent.getWidth() / 3, (int)parent.getWidth() / 3);
                convertView.setLayoutParams(params);
            } else {
                params.height = (int)parent.getWidth() / 3;
                params.width = (int)parent.getWidth() / 3;
            }
            holder.checkBox=(CheckBox)convertView.findViewById(R.id.cb_image_selected);
            holder.checkBox.setClickable(false);
            holder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
            final ViewHolder finalHolder = holder;
            holder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finalHolder.checkBox.setChecked(!finalHolder.checkBox.isChecked());
                    list.get(position).setChecked(finalHolder.checkBox.isChecked());
                    Log.d("aaa",String.valueOf(list.get(position).isChecked()));
                }
            });

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
          *//*  Bitmap bitmap=zoomImg(photoList.get(position),100,100);
            holder.imageView.setImageBitmap(bitmap);*//*
        Glide.with(mContext)
                .load(list.get(position).getUrl())
                .placeholder(R.mipmap.ic_gf_default_photo)
                .centerCrop()
                .crossFade()
                .error(R.mipmap.fail)
                .into(holder.imageView);*/
        return convertView;
    }

    protected class ViewHolder {
        protected ImageView imageView;
        protected CheckBox checkBox;
    }
    public static Bitmap zoomImg(Bitmap bm, int newWidth , int newHeight){
        // 获得图片的宽高
        int width = bm.getWidth();
        int height = bm.getHeight();
        // 计算缩放比例
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.reset();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的图片
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        return newbm;
    }
}
